package bim.service;

import java.util.Iterator;
import java.util.List;

import keter.domain.Role;
import keter.domain.Unit;
import keter.exception.KeterValidationException;
import keter.service.RoleService;
import keter.service.UnitService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bim.dao.UserDao;
import bim.domain.User;

@Service
public class ImportService {
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(ImportService.class);

    @Autowired
    private UserDao dao;
    
    @Autowired
    private RoleService rs;
    
    @Autowired
    private UnitService us;
    
    public void importUser(List<User> list) throws KeterValidationException {
			for (Iterator<User> iterator = list.iterator(); iterator.hasNext();) {
				User user = (User) iterator.next();
				Role role = rs.findByName(user.getAccount().getRoleNames());
				user.getAccount().setRole(role);
				Unit unit = us.findByCode(user.getAccount().getUnit().getCode());
				user.getAccount().setUnit(unit);
			}
				dao.saveList(list);
    }
}
